//
//  BCStuffInfoInteractiveCollectionViewCell.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/6/29.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCStuffInfoInteractiveCollectionViewCell.h"

@class _MMNumberKeyboardButton;

@interface BCStuffInfoInteractiveCollectionViewCell () <UITextFieldDelegate,MMNumberKeyboardDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *heartImageView;
@property (weak, nonatomic) IBOutlet UIButton *type1Button;
@property (weak, nonatomic) IBOutlet UIButton *type2Button;
@property (weak, nonatomic) IBOutlet UIButton *type3Button;
@end

@implementation BCStuffInfoInteractiveCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _type1Button.layer.cornerRadius = 3.f;
    _type1Button.layer.borderWidth = 1.f;
    _type1Button.layer.borderColor = BangcaiGreen.CGColor;
    _type2Button.layer.cornerRadius = 3.f;
    _type2Button.layer.borderWidth = 1.f;
    _type2Button.layer.borderColor = BangcaiGreen.CGColor;
    _type3Button.layer.cornerRadius = 3.f;
    _type3Button.layer.borderWidth = 1.f;
    _type3Button.layer.borderColor = BangcaiGreen.CGColor;
    
    // Create and configure the keyboard.
    MMNumberKeyboard *keyboard = [[MMNumberKeyboard alloc] initWithFrame:CGRectZero];
    keyboard.returnKeyTitle = @"确认";
    NSDictionary *dic = [keyboard valueForKey:@"buttonDictionary"];
    [dic[@(12)] setValue:BangcaiGreen forKey:@"fillColor"];
    keyboard.allowsDecimalPoint = NO;
    keyboard.delegate = self;
    _countTextFeild.inputView = keyboard;
    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40.f)];
    accessoryView.backgroundColor = [UIColor whiteColor];
    UILabel *keyboardTipLable = [[UILabel alloc] initWithFrame:accessoryView.bounds];
    keyboardTipLable.font = [UIFont systemFontOfSize:13];
    keyboardTipLable.textColor = BangcaiGreen;
    keyboardTipLable.text = @"请输入欲购买的数量";
    keyboardTipLable.textAlignment = NSTextAlignmentCenter;
    [accessoryView addSubview:keyboardTipLable];
    _countTextFeild.inputAccessoryView = accessoryView;
}

- (void)makeAttributedString {
    
    NSString *priceString = [NSString stringWithFormat:@"均价 %@",_priceLable.text];
    NSMutableAttributedString *rmbAttributedString = [[NSMutableAttributedString alloc] initWithString:priceString];
    UIColor *titleColor = [UIColor lightGrayColor];
    UIColor *rmbColor = [UIColor colorWithRed:0.933 green:0.298 blue:0.216 alpha:1.000];
    [rmbAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                                value:titleColor
                                range:NSMakeRange(0, @"均价 ".length)];
    [rmbAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                                value:rmbColor
                                range:NSMakeRange(@"均价 ".length, _priceLable.text.length)];
    UIFont *titleFont = [UIFont systemFontOfSize:12];
    UIFont *rmbFont = [UIFont systemFontOfSize:17];
    if (iPhone4_4S) {
        titleFont = [UIFont systemFontOfSize:10];
        rmbFont = [UIFont systemFontOfSize:15];
    }
    [rmbAttributedString addAttribute:NSFontAttributeName             //文字字体
                                value:titleFont
                                range:NSMakeRange(0, @"均价 ".length+@"￥".length)];
    [rmbAttributedString addAttribute:NSFontAttributeName             //文字字体
                                value:rmbFont
                                range:NSMakeRange(@"均价 ".length+@"￥".length, _priceLable.text.length-@"￥".length)];
    _priceLable.attributedText = rmbAttributedString;
    _perLable.font = titleFont;
}

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView {
    static NSString *ID = @"StuffInfoInteractiveCell";
    UINib *nib = [UINib nibWithNibName:@"BCStuffInfoInteractiveCollectionViewCell" bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:ID];
    BCStuffInfoInteractiveCollectionViewCell *cell = [[BCStuffInfoInteractiveCollectionViewCell alloc] init];
    return cell;
}

- (IBAction)typeAction:(UIButton *)sender {
    if (sender.tag == 1) {
        [_type1Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_type1Button setBackgroundColor:BangcaiGreen];
        [_type2Button setTitleColor:BangcaiGreen forState:UIControlStateNormal];
        [_type2Button setBackgroundColor:[UIColor whiteColor]];
        [_type3Button setTitleColor:BangcaiGreen forState:UIControlStateNormal];
        [_type3Button setBackgroundColor:[UIColor whiteColor]];
    }
    else if (sender.tag == 2) {
        [_type1Button setTitleColor:BangcaiGreen forState:UIControlStateNormal];
        [_type1Button setBackgroundColor:[UIColor whiteColor]];
        [_type2Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_type2Button setBackgroundColor:BangcaiGreen];
        [_type3Button setTitleColor:BangcaiGreen forState:UIControlStateNormal];
        [_type3Button setBackgroundColor:[UIColor whiteColor]];
    }
    else {
        [_type1Button setTitleColor:BangcaiGreen forState:UIControlStateNormal];
        [_type1Button setBackgroundColor:[UIColor whiteColor]];
        [_type2Button setTitleColor:BangcaiGreen forState:UIControlStateNormal];
        [_type2Button setBackgroundColor:[UIColor whiteColor]];
        [_type3Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_type3Button setBackgroundColor:BangcaiGreen];
    }
}

- (IBAction)addAction:(id)sender {
    NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
    STUFF *stuff_model = stuff_array.lastObject;
    [[BCShopCarManager sharedInstance] addStuffName:self.stuffNameLable.text amount:1 price:stuff_model.price.doubleValue type:stuff_model.category.integerValue];
    [BCOrderViewManager updateAddStuffToOrder:stuff_model];
    
    _countTextFeild.text = [NSString stringWithFormat:@"%@",@(_countTextFeild.text.integerValue+1)];
    
    _reddot();
}

- (IBAction)decAction:(id)sender {
    if (_countTextFeild.text.integerValue>1) {
        NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
        STUFF *stuff_model = stuff_array.lastObject;
        _countTextFeild.text = [NSString stringWithFormat:@"%@",@(_countTextFeild.text.integerValue-1)];
        [[BCShopCarManager sharedInstance] removeStuffName:self.stuffNameLable.text amount:1 price:stuff_model.price.doubleValue type:stuff_model.category.integerValue];
        [BCOrderViewManager updateDecStuffToOrder:stuff_model];
    }
}

- (IBAction)heartAction:(id)sender {
    if ([_heartImageView.image isEqual:[UIImage imageNamed:@"选中-红心"]]) {
        _heartImageView.image = [UIImage imageNamed:@"未选择-灰"];
    }
    else {
        _heartImageView.image = [UIImage imageNamed:@"选中-红心"];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    _addButton.userInteractionEnabled = NO;
    _decButton.userInteractionEnabled = NO;
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    _addButton.userInteractionEnabled = YES;
    _decButton.userInteractionEnabled = YES;
}

-(BOOL)numberKeyboardShouldReturn:(MMNumberKeyboard *)numberKeyboard {
    
    NSArray *stuff_array = [BCShopViewManager readDataFromStuff:_uuid];
    STUFF *stuff_model = stuff_array.lastObject;
    if (0 == _countTextFeild.text.integerValue) {
        _decButton.hidden = YES;
        _countTextFeild.hidden = YES;
        [BCOrderViewManager removeOrder:_uuid];
    }
    else {
        [BCOrderViewManager writeStuffUseCountToOrder:_uuid Count:_countTextFeild.text.integerValue];
    }
    
    [[BCShopCarManager sharedInstance] setStuffName:_countTextFeild.text amount:_countTextFeild.text.integerValue price:stuff_model.price.doubleValue type:stuff_model.category.integerValue];
    return YES;
}

@end
